package com.example.jwttoken.controller;

import com.example.jwttoken.service.PoiService;
import com.example.jwttoken.utils.POIUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述:
 *
 * @ClassName: PoiController
 * @Author: ZhangHao
 * @Date: 2019-03-18 9:52
 * @Version: V1.0
 */
@Controller
@RequestMapping("/poi")
public class PoiController {
    @Autowired
    private PoiService poiService;
    @RequestMapping("/noVerificationForExcel")
    public void registerByNoVerificationForExcel(HttpServletRequest request, HttpServletResponse response, @RequestParam MultipartFile file) {
        List<ArrayList<String>> readResult = null;//总行记录
        try {

            //判断文件是否为空
            if (file.isEmpty()) {
                System.out.println("空");
            }
            //判断文件大小
            long size = file.getSize();
            String name = file.getOriginalFilename();
            if (StringUtils.isBlank(name) || size == 0) {
                System.out.printf("上传文件为空");
            }
            //获取文件后缀
            String postfix = POIUtil.getPostfix(name);


            //读取文件内容
            if (StringUtils.equals("xlsx", postfix)) {
                readResult = POIUtil.readXlsx(file);
            } else if (StringUtils.equals("xls", postfix)) {
                readResult = POIUtil.readXls(file);
            } else {
                System.out.printf("文件错误");
            }

            if (readResult == null || readResult.size() == 0) {
                System.out.println("解析失败");
            }

            for (ArrayList<String> arr : readResult) {
                String account = arr.get(6);//获取指定单元格的内容，这边我只获取了其中一列的数据
                if (StringUtils.isBlank(account)) {
                    continue;
                }

                /**调用service进行具体的业务操作 */
                //...
            }


        } catch (Exception e) {

        }
    }
    @RequestMapping("/upload")
    public String upload () {
        return "upload";
    }

}
